<?php
namespace api\modules\v1\controllers;
use api\common\controllers\UserController as U;
use api\common\models\User;
use yii\web\HttpException;
use yii\web\NotFoundHttpException;
use yii\web\ServerErrorHttpException;
use Yii;
use yii\db\Expression;

class WithdrawController extends U
{
    public $modelClass = 'api\modules\v1\models\Withdraw';

    public function actions()
    {
        $actions = parent::actions();
        unset($actions['create']); //卸载操作

        return $actions;
    }


    /*
     * 提现申请
     * @return array
     * @throws HttpException
     * @throws ServerErrorHttpException
     */
    public function actionCreate(){
        if(Yii::$app->user->identity->phone == Yii::$app->params['alipay_public_account']){
            throw new HttpException(401,'此账户不能提现',0);
        }
        $model = new $this->modelClass();
        $post = Yii::$app->request->post();
        if($post['withdraw_type'] != 1 && $post['withdraw_type'] != 2){
            throw new HttpException(401,'无效的参数',0);
        }
        if($post['money'] < 100){
            throw new HttpException(401,'提现金额不得小于100',0);
        }
        $user_balance = User::find()->select('balance')->where(['id' => Yii::$app->user->identity->id])->asArray()->one();
        if($user_balance['balance'] < $post['money'] ){
            throw new HttpException(401,'提现金额不得大于可提现余额',0);
        }
        //提现算法
        $poundage = sprintf("%.2f", $post['money']*0.15);
        if($post['money'] == $user_balance['balance'] || ($post['money'] + $poundage) > $user_balance['balance']){
            $post['money'] = sprintf("%.2f", $user_balance['balance']/1.15);//到账金额
            $post['poundage'] = $user_balance['balance'] - $post['money'];//手续费
        }else{
            $post['poundage'] = sprintf("%.2f", $post['money']*0.15);
        }
        $model->attributes = $post;
        $model->code = 'TX'.date('YmdHis',time()).str_pad(Yii::$app->user->identity->id,7, "0", STR_PAD_LEFT);
        $model->userid = Yii::$app->user->identity->id;
        $model->time = date("Y-m-d H:i:s",time());
        $model->poundage = $post['poundage'];//手续费
        $model->status = 0;
        if($model->save()){
            User::updateAll(['balance'=> new Expression('`balance` - '.($model->money + $model->poundage))],['id'=>Yii::$app->user->identity->id]);
            return [
                "message" => "申请成功!请等待处理",
            ];
        }elseif (!$model->hasErrors()) {
            throw new ServerErrorHttpException('由于不明原因未能创建对象.');
        }else{
            throw new HttpException(401,array_values($model->getFirstErrors())[0],0);
        }
    }

    /*
     * 提现记录
     */
    public function actionList(){
        $model = new $this->modelClass();
        return $model->getList();
    }
    /*
     * 提现规则
     */
    public function actionRules(){
        $connection = \Yii::$app->db;
        $rules= $connection->createCommand('SELECT * FROM {{%withdraw_rules_app}}');
        $rules = $rules->queryOne();
        return $rules;
    }
}